var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		st:'all',
		datalist: [],
		pagenum: 1
	},
	_onLoad:function(){
		var that = this
		that.setData({ pagenum: 1,datalist: [], $nomoreData: false,session_id:$.config.session_id});
		that.getDataList();
	},
	changetab:function(e){
		var st = e.currentTarget.dataset.st
		var that = this
		that.setData({ pagenum: 1,st:st, datalist: [], $nomoreData: false });
		that.getDataList();
	},
	getDataList: function () {
		var that = this
		var pagenum = that.data.pagenum
		$.post($.pre_url + '/am.php?s=/product/index', { pagenum: pagenum,st:that.data.st}, function (res) {
			var data = res.datalist
			if (pagenum == 1) {
				that.setData({ datalist: data,count0:res.count0,count1:res.count1,countall:res.countall});
				if (data.length == 0) {
					$.showEmpty('暂无相关数据！');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
			that.setData({
				sclist: data
			});
		});
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
	},
	todel:function (e) {
		var that = this
		var id = e.currentTarget.dataset.id
		$.confirm('确定要删除该商品吗?', function () {
			$.post($.pre_url + '/am.php?s=/product/del', { id: id }, function (res){
				if(res.status == 1){
					$.success(res.msg);
					that._onLoad();
				}else{
					$.error(res.msg);
				}
			});
		})
	},
	setst:function (e){
		var that = this
		var id = e.currentTarget.dataset.id
		var st = e.currentTarget.dataset.st
		$.confirm('确定要'+(st==0?'下架':'上架')+'吗?', function () {
			$.post($.pre_url + '/am.php?s=/product/setst', { st:st,id: id }, function (res) {
				if(res.status == 1){
					$.success(res.msg);
					that._onLoad();
				}else{
					$.error(res.msg);
				}
			});
		});
	}
}))